<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>${action == 'add' ? '添加场次' : '编辑场次'} - 管理员后台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .admin-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 2rem 0;
        }
        .form-card {
            border: none;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            border-radius: 10px;
        }
        .form-label {
            font-weight: 600;
            color: #495057;
        }
        .required {
            color: #dc3545;
        }
    </style>
</head>
<body class="bg-light">
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
        <div class="container">
            <a class="navbar-brand fw-bold" href="${pageContext.request.contextPath}/admin/">
                <i class="fas fa-cog me-2"></i>管理员后台
            </a>

            <div class="navbar-nav ms-auto">
                <!-- 主要管理功能 -->
                <a class="nav-link" href="${pageContext.request.contextPath}/admin/movies">
                    <i class="fas fa-film me-1"></i>电影管理
                </a>
                <a class="nav-link" href="${pageContext.request.contextPath}/admin/showtimes">
                    <i class="fas fa-calendar-alt me-1"></i>场次管理
                </a>
                <a class="nav-link" href="${pageContext.request.contextPath}/movies">
                    <i class="fas fa-home me-1"></i>前台首页
                </a>

                <!-- 用户菜单 -->
                <div class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">
                        <i class="fas fa-user me-1"></i>${sessionScope.user.username}
                    </a>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/admin/statistics">
                            <i class="fas fa-chart-bar me-2"></i>统计报表
                        </a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/logout">
                            <i class="fas fa-sign-out-alt me-2"></i>退出登录
                        </a></li>
                    </ul>
                </div>
            </div>
        </div>
    </nav>

    <!-- 管理员头部 -->
    <section class="admin-header">
        <div class="container">
            <h2><i class="fas fa-calendar-alt me-2"></i>${action == 'add' ? '添加场次' : '编辑场次'}</h2>
            <p class="mb-0">${action == 'add' ? '为电影安排新的放映场次' : '修改现有场次信息'}</p>
        </div>
    </section>

    <div class="container mt-4">
        <!-- 错误信息 -->
        <c:if test="${not empty error}">
            <div class="alert alert-danger" role="alert">
                <i class="fas fa-exclamation-triangle me-2"></i>${error}
            </div>
        </c:if>

        <!-- 返回按钮 -->
        <div class="mb-3">
            <a href="${pageContext.request.contextPath}/admin/showtimes" class="btn btn-outline-secondary">
                <i class="fas fa-arrow-left me-2"></i>返回场次列表
            </a>
        </div>

        <!-- 表单 -->
        <div class="card form-card">
            <div class="card-body p-4">
                <form method="post" action="${pageContext.request.contextPath}/admin/showtimes">
                    <input type="hidden" name="action" value="${action}">
                    <c:if test="${action == 'edit'}">
                        <input type="hidden" name="id" value="${showtime.id}">
                    </c:if>

                    <div class="row">
                        <!-- 电影选择 -->
                        <div class="col-md-6 mb-3">
                            <label for="movieId" class="form-label">电影 <span class="required">*</span></label>
                            <select class="form-select" id="movieId" name="movieId" required>
                                <option value="">请选择电影</option>
                                <c:forEach var="movie" items="${movies}">
                                    <option value="${movie.id}"
                                            ${showtime.movieId == movie.id ? 'selected' : ''}>
                                        ${movie.title}
                                    </option>
                                </c:forEach>
                            </select>
                        </div>

                        <!-- 影院选择 -->
                        <div class="col-md-6 mb-3">
                            <label for="cinemaId" class="form-label">影院 <span class="required">*</span></label>
                            <select class="form-select" id="cinemaId" name="cinemaId" required onchange="loadHalls()">
                                <option value="">请选择影院</option>
                                <c:forEach var="cinema" items="${cinemas}">
                                    <option value="${cinema.id}"
                                            ${showtime.cinemaId == cinema.id ? 'selected' : ''}>
                                        ${cinema.name}
                                    </option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="row">
                        <!-- 影厅选择 -->
                        <div class="col-md-6 mb-3">
                            <label for="hallId" class="form-label">影厅 <span class="required">*</span></label>
                            <select class="form-select" id="hallId" name="hallId" required>
                                <option value="">请先选择影院</option>
                                <c:forEach var="hall" items="${halls}">
                                    <option value="${hall.id}"
                                            data-cinema-id="${hall.cinemaId}"
                                            ${showtime.hallId == hall.id ? 'selected' : ''}>
                                        ${hall.name} (${hall.totalSeats}座)
                                    </option>
                                </c:forEach>
                            </select>
                        </div>

                        <!-- 票价 -->
                        <div class="col-md-6 mb-3">
                            <label for="price" class="form-label">票价 <span class="required">*</span></label>
                            <div class="input-group">
                                <span class="input-group-text">¥</span>
                                <input type="number" class="form-control" id="price" name="price"
                                       step="0.01" min="0" max="999.99" required
                                       value="${showtime.price}">
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <!-- 放映日期 -->
                        <div class="col-md-6 mb-3">
                            <label for="showDate" class="form-label">放映日期 <span class="required">*</span></label>
                            <input type="date" class="form-control" id="showDate" name="showDate" required
                                   value="<fmt:formatDate value='${showtime.showDate}' pattern='yyyy-MM-dd'/>">
                        </div>

                        <!-- 放映时间 -->
                        <div class="col-md-6 mb-3">
                            <label for="showTime" class="form-label">放映时间 <span class="required">*</span></label>
                            <input type="time" class="form-control" id="showTime" name="showTime" required
                                   value="<fmt:formatDate value='${showtime.showTime}' pattern='HH:mm'/>">
                        </div>
                    </div>

                    <div class="row">
                        <!-- 预售开始时间 -->
                        <div class="col-md-6 mb-3">
                            <label for="presaleStart" class="form-label">预售开始时间</label>
                            <input type="datetime-local" class="form-control" id="presaleStart" name="presaleStart"
                                   value="<fmt:formatDate value='${showtime.presaleStart}' pattern='yyyy-MM-dd&apos;T&apos;HH:mm'/>">
                        </div>

                        <!-- 订票截止时间 -->
                        <div class="col-md-6 mb-3">
                            <label for="bookingEnd" class="form-label">订票截止时间</label>
                            <input type="datetime-local" class="form-control" id="bookingEnd" name="bookingEnd"
                                   value="<fmt:formatDate value='${showtime.bookingEnd}' pattern='yyyy-MM-dd&apos;T&apos;HH:mm'/>">
                        </div>
                    </div>

                    <!-- 状态 -->
                    <div class="mb-3">
                        <label for="status" class="form-label">状态 <span class="required">*</span></label>
                        <select class="form-select" id="status" name="status" required>
                            <option value="SCHEDULED" ${showtime.status == 'SCHEDULED' ? 'selected' : ''}>已安排</option>
                            <option value="SELLING" ${showtime.status == 'SELLING' ? 'selected' : ''}>售票中</option>
                            <option value="SOLD_OUT" ${showtime.status == 'SOLD_OUT' ? 'selected' : ''}>已售罄</option>
                            <option value="CANCELLED" ${showtime.status == 'CANCELLED' ? 'selected' : ''}>已取消</option>
                            <option value="ENDED" ${showtime.status == 'ENDED' ? 'selected' : ''}>已结束</option>
                        </select>
                    </div>

                    <!-- 提交按钮 -->
                    <div class="text-end">
                        <button type="button" class="btn btn-secondary me-2" onclick="history.back()">取消</button>
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save me-2"></i>${action == 'add' ? '添加场次' : '保存修改'}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        // 存储所有影厅数据
        const allHalls = [
            <c:forEach var="hall" items="${halls}" varStatus="status">
            {
                id: ${hall.id},
                name: '${hall.name}',
                cinemaId: ${hall.cinemaId},
                totalSeats: ${hall.totalSeats}
            }<c:if test="${!status.last}">,</c:if>
            </c:forEach>
        ];

        function loadHalls() {
            const cinemaId = document.getElementById('cinemaId').value;
            const hallSelect = document.getElementById('hallId');
            const currentHallId = '${showtime.hallId}';

            // 清空影厅选项
            hallSelect.innerHTML = '<option value="">请选择影厅</option>';

            if (cinemaId) {
                // 添加对应影院的影厅
                allHalls.forEach(hall => {
                    if (hall.cinemaId == cinemaId) {
                        const option = document.createElement('option');
                        option.value = hall.id;
                        option.textContent = hall.name + ' (' + hall.totalSeats + '座)';

                        // 如果是编辑模式，保持原选中状态
                        if (hall.id == currentHallId) {
                            option.selected = true;
                        }

                        hallSelect.appendChild(option);
                    }
                });
            }
        }

        // 页面加载时初始化影厅列表
        document.addEventListener('DOMContentLoaded', function() {
            loadHalls();
        });
    </script>
</body>
</html>
